package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import utils.events.MenuEvent;

	/**
	 *带上下滚动条的Sprite
	 * by limaolin
	 */
	public class ScrollBarSprite extends Sprite
	{
		//滚动条的条
		private var mcScrollLine:Sprite;
		//滚动条的拖动按钮
		private var mcScrollButton:Sprite;
		//用来遮罩的Sprite
		private var mcMask:Sprite;
		//包含显示内容的sprite
		private var mcContent:Sprite;
		//要显示内容的宽度
		private var showWidth:int;
		//要显示内容的高度
		private var showHeight:int;
		//拖动按钮的x坐标
		private var buttonX:int=0;

		/**
		 * 构造函数
		 * @param line 滚动条的条
		 * @param btn 滚动条的拖动按钮
		 * @param content 包含显示内容的sprite
		 * @param w 要显示内容的宽度
		 * @param h 要显示内容的高度
		 * @param btnYOffset 拖动按钮的y坐标针对w的偏移像素
		 * @param lineYOffset 滚动条的条的y坐标针对w的偏移像素
		 */
		public function ScrollBarSprite(line:Sprite, btn:Sprite, content:Sprite, w:int, h:int, btnYOffset:Number=0, lineYOffset:Number=0)
		{
			super();

			//给内部对象赋值
			this.showWidth=w;
			this.showHeight=h;
			this.mcScrollLine=line;
			this.mcScrollButton=btn;
			this.mcContent=content;
			//初始化遮罩
			this.mcMask=new Sprite();
			this.mcMask.graphics.beginFill(0x000000);
			this.mcMask.graphics.drawRect(0, 0, w, h);
			this.mcMask.graphics.endFill();
			this.addChild(this.mcContent);
			this.addChild(this.mcMask);
			this.mcContent.mask=this.mcMask;

			if (true||this.mcContent.height > h)
			{
				//设置滚动条
				this.mcScrollLine.x=(w - this.mcScrollLine.width) + lineYOffset;
				this.mcScrollButton.x=(w - this.mcScrollButton.width) + btnYOffset;
				buttonX=this.mcScrollButton.x;
				this.addChild(this.mcScrollLine);
				this.addChild(this.mcScrollButton);

				mcScrollButton.addEventListener(MouseEvent.MOUSE_OUT, ButtonMouseOutHandler);
				mcScrollButton.addEventListener(MouseEvent.MOUSE_DOWN, ButtonMouseDownHandler);
				mcScrollButton.addEventListener(MouseEvent.MOUSE_UP, ButtonMouseUpHandler);

			}
			this.addEventListener(MenuEvent.onMenuItemClick, onMenuItemClickHandler);
		}
		protected function onMenuItemClickHandler(event:MenuEvent):void
		{
			// TODO Auto-generated method stub
			trace("ScrollBarSprite receive item click");
			trace(" this.mcContent.y:"+this.mcContent.y+" content can move:"+(this.mcContent.height -  this.mcMask.height)+" this.mcScrollButton.y:"+this.mcScrollButton.y+" scroll can move:"+(this.mcScrollLine.height - mcScrollButton.height));
			if(-this.mcContent.y>this.mcContent.height -  this.mcMask.height){
				this.mcContent.y=  this.mcMask.height-this.mcContent.height
			}
			var per:Number=this.mcContent.y * 100 / (this.mcContent.height -  this.mcMask.height);
			//trace(this.mcScrollButton.y);
			this.mcScrollButton.y=-(this.mcScrollLine.height - mcScrollButton.height) * per / 100;
			trace(" this.mcContent.y:"+this.mcContent.y+" this.mcScrollButton.y:"+this.mcScrollButton.y);
		}


		private function ButtonMouseOutHandler(e:MouseEvent):void
		{
			mcScrollButton.stopDrag();
			placeContent();
			//this.removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			if (this.mcContent.height <= this.mcMask.height)
			{
				//trace("content < mask");
				this.mcScrollButton.y=0;
			}
		}

		private function ButtonMouseDownHandler(e:MouseEvent):void
		{
			var Rect:Rectangle=new Rectangle(buttonX, 0, 0, mcScrollLine.height - mcScrollButton.height);
			mcScrollButton.startDrag(false, Rect);
			//this.addEventListener(Event.ENTER_FRAME, EnterFrameHandler);	
		}
		
		private function placeContent():void
		{
			if (this.mcContent.height <= this.mcMask.height)
			{
				return;		
			}
			else
			{
				var per:Number=this.mcScrollButton.y * 100 / (this.mcScrollLine.height - mcScrollButton.height);
				//trace(this.mcScrollButton.y);
				this.mcContent.y=-(this.mcContent.height - this.mcMask.height) * per / 100;
			}
		}
		
		private function ButtonMouseUpHandler(e:MouseEvent):void
		{
			mcScrollButton.stopDrag();
			placeContent();
			//this.removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			if (this.mcContent.height <= this.mcMask.height)
			{
				//trace("content < mask");
				this.mcScrollButton.y=0;
			}

		}

		private function EnterFrameHandler(e:Event):void
		{
			placeContent();
		}
	}
}
